
goog.provide('esn.AppContext');

goog.require('goog.net.XhrManager');
goog.require('goog.object');


/**
 * @constructor
 */
esn.AppContext = function(user, isDevMode) {
  this.user_ = user;

  this.isDevMode_ = isDevMode;

  this.services_ = {};
};

/**
 * @type {goog.net.XhrManager}
 */
esn.AppContext.prototype.xhrManager_;

esn.AppContext.prototype.isDevMode = function() {
  return this.isDevMode_;
};

esn.AppContext.prototype.getUser = function() {
  return this.user_;
};

esn.AppContext.prototype.registerService = function(id, service) {
  if (!goog.object.containsValue(esn.constants.ServiceId, id)) {
    throw new Error('Undefined service id: ' + id);
  } else if (id in this.services_) {
    throw new Error('Service is already registered (id = ' + id + ').');
  }
  this.services_[id] = service;
};

esn.AppContext.prototype.getService = function(id) {
  if (id in this.services_) {
    return this.services_[id];
  }
  throw new Error('Service is not registered (id = ' + id + ')');
};
    
esn.AppContext.prototype.getXhrManager = function() {
  if (!this.xhrManager_) {
    this.xhrManager_ = new goog.net.XhrManager();
  }
  return this.xhrManager_;
};